<template>
  <div class="loginContainer">
    <el-card header="欢迎注册就爱shop商城">
      <el-form
        :model="formData"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        label-position="left"
        :data="tabledata"
      >
        <el-form-item label="账号：" prop="username">
          <el-input
            type="text"
            v-model="formData.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input
            type="password"
            v-model="formData.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="确认密码：" prop="confirmpassword">
          <el-input
            type="password"
            v-model="formData.confirmpassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button type="primary" @click="clearInput"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { Add } from "../api/users";
export default {
  data() {
    return {
      tabledata: [],
      formData: {
        username: "",
        password: "",
        phone: "",
        address: "",
        consignee: "",
        confirmpassword: "",
      },
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入你的账号！", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入你的密码！", trigger: "blur" },
        ],
        confirmpassword: [
          { required: true, message: "请再次输入你的密码！", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      if (this.formData.password == this.formData.confirmpassword) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            Add(this.formData).then(({ data }) => {
              console.log(data);
              this.tabledata.push(data.data);
              this.$message.success("注册成功！");
              this.$router.push("/login");
            });
          }
        });
      } else {
        console.log("error submit!!");
        this.$message.error("两次密码不相同！");
        return false;
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    clearInput(){
      this.formData.password = "";
      this.formData.confirmpassword = "";
    }
  },
};
</script>

<style>
body {
  background-color: white;
}
.loginContainer {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 50vh;
  font-weight: 900;
}
.el-card__header {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 26px;
  font-weight: 900;
}
.el-card__body {
  width: 400px;
  /* background-image: url("./imgs/BackImg.png");
  opacity: 0.7; */
}
</style>